package com.fz.controller;

import com.fz.entity.*;
import com.fz.mapper.WebArtworkMapper;
import com.fz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Z-C-Y on 2018-03-05.
 * artwork.jsp   ，艺术品
 */
@Controller
public class WebArtworkController {
    @Autowired
    protected WebArtworkMapper webArtistMapper;

    @RequestMapping(value = "/webpage/artwork")
    public String artwork(Model model, HttpServletRequest req) {
        /*艺术品==条件查询====画品分类*/
        List<Type> list = webArtistMapper.queryHuaPin();
        model.addAttribute("listType", list);

      /*  艺术品==条件查询====题材分类*/
        List<Theme> list1 = webArtistMapper.queryTiCai();
        model.addAttribute("listTheme",list1);

       /* 艺术品==条件查询====适合场所*/
        List<Site> list2 = webArtistMapper.querySite();
        for(Site ss:list2){
            System.out.println("适合场所====="+ss.toString() );
        }
        model.addAttribute("listOccation",list2);

       /* 艺术品==条件查询====价格范围*/
        List<Ranges> list3 = webArtistMapper.queryRanges();
        model.addAttribute("listPrice",list3);

        /*分页*/
        /*实现分页并查询的功能*/
        int  pageNun1 = 1;
        if((req.getParameter("pageNun") != null) && !("".equals(req.getParameter("pageNun")))){
            pageNun1 = Integer.parseInt(req.getParameter("pageNun"));
        }
        //System.out.println("==获得当前是第几页==pageNun===="+req.getParameter("pageNun"));
        //每页显示的记录数
        int pageSize = 8;
        /*分页======查询数据库的数据的总数据*/
        List<Product> list4 = webArtistMapper.queryProduct();
        model.addAttribute("list4Product",list4);

        /*得到表中总条数*/
        int totalRecord = list4.size();

        //有了初始化数据，就可以创建PageBean对象了
        PageBean pbm = new PageBean(pageNun1, pageSize, totalRecord);

        model.addAttribute("pbm",pbm);
        //获取PageBeanManage对象中的startIndex;
        int startIndex = pbm.getStartIndex();

        //有了pageSize和startIndex，就可以拿到每页的数据
        List<Product> list5 =  webArtistMapper.queryProductData(startIndex,pageSize);
        model.addAttribute("list5",list5);
        model.addAttribute("t","artwork");
        return "webpage/artwork";
    }

}
